<script setup lang="ts">
import {Search} from "@element-plus/icons-vue";
import * as ProductGroupApi from '@/api/base/productgroup';
import {DICT_TYPE} from "@/utils/dict";
import { handleTree } from '@/utils/tree'
import * as DocApi from "@/api/wms/doc";

const productGroupId = defineModel("productGroupId");


const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref("选择产品群组")


const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const productGroupName = ref(undefined)
const queryFormRef = ref() // 搜索的表单
const queryParams = reactive({
  name: undefined,
  code: undefined,
  status: 0
})

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ProductGroupApi.getProductGroupList(queryParams)
    list.value = handleTree(data, 'id', 'parentId')
  } finally {
    loading.value = false
  }
}
/** 展示弹出框 */
const openDialog = () => {
  dialogVisible.value = true;
  getList();
}

/** 关闭弹出框 */
const closeDialog = () => {
  dialogVisible.value = false;
  queryFormRef.value.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields();
  handleQuery();
}
/** 选择操作 */
const selected = (row) => {
  productGroupId.value = row.id;
  productGroupName.value = row.name;
  closeDialog();
}
/** 行点击事件*/
const rowClick = (row, column, event) => {
  selected(row);
}

watch(productGroupId, (newVal) => {
  if (newVal === null || newVal === undefined) {
    productGroupName.value = undefined;
  }else {
   ProductGroupApi.getProductGroup(newVal).then(response => {
     productGroupName.value = response.name;
   })
  }
})

</script>

<template>
  <!-- 输入框 -->
  <el-input readonly v-model="productGroupName" placeholder="请选择产品群组">
    <template #append>
      <el-button :icon="Search" @click="openDialog()" />
    </template>
  </el-input>
  <!-- 弹窗 -->
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="60%">
    <!-- 搜索工作栏 -->
    <div class="mb-30px">
      <el-form
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="68px"
      >
        <el-form-item label="群组名称" prop="name">
          <el-input
              v-model="queryParams.name"
              placeholder="请输入群组名称"
              clearable
              @keyup.enter="handleQuery"
              class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="群组编号" prop="code">
          <el-input
              v-model="queryParams.code"
              placeholder="请输入群组编号"
              clearable
              @keyup.enter="handleQuery"
              class="!w-240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="content">
      <el-table v-loading="loading" :data="list" :stripe="true" row-key="id" :show-overflow-tooltip="true" border max-height="500" @row-dblclick="rowClick">
        <el-table-column label="群组编号" align="center" prop="code" />
        <el-table-column label="群组名称" align="center" prop="name" />
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="显示顺序" align="center" prop="sort" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button
                link
                type="primary"
                @click="selected(scope.row)"
            >
              选择
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

  </Dialog>
</template>

<style scoped lang="scss">
.content {
  overflow: hidden;
}
</style>
